package com.mux.java8.list;

import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;


/**
 * Lambda List
 * @author devin.chen
 *
 */
public class ListExp {
	public static void main(String[] args) {
		List<String> list = Arrays.asList("jack","devin","debx","devin","zbx","abili");
		/**
		 * 1. 对List 集合排序
		 */
		list.sort( (str1,str2) -> {
			int result = str1.compareTo(str2);
			return result;
		});
		/**
		 * 2.遍历list 集合,并输出
		 */
		list.forEach(System.out::println);
		
		/**
		 * 3. list 集合过滤
		 */
		list.stream().filter(str->str.startsWith("d")).forEach(System.out::println);
		
		/**
		 * 4.对filter 结果转为一个数组
		 */
		System.out.println(Arrays.toString(list.stream().filter(s->s.startsWith("d")).toArray()));
		
		/**
		 * 5. list  stream 排序 sorted()
		 */
		list.stream().sorted().filter(s->s.startsWith("d")).forEach(System.out::println);
		
		/**
		 * 6.stream map函数
		 */
		list.stream().map(String::toUpperCase).sorted((a,b)->b.compareTo(a)).forEach(System.out::println);
		list.stream().map(name->name.length()).forEach(count->System.out.println(count));
		//使用方法引用
		list.stream().map(String::toUpperCase).forEach(name->System.out.println(name));
		
		/**
		 * collect
		 */
		List<String> startWithN = list.stream().filter(name->name.startsWith("d")).collect(Collectors.toList());
	
		
		/**
		 * collectors 使用
		 */
		List<Article> artList = getArticleList();
		/**
		 * 拼接字符串
		 */
		String str = artList.stream().map(Article::getTitle).collect(Collectors.joining(",", "[", "]"));
		System.out.println(str);
		/**
		 * 分组
		 */
		Map<String,List<Article>> artMap = artList.stream().collect(Collectors.groupingBy(Article::getTitle));
		
	
	
	}
	
	public static List<Article> getArticleList(){
		List<Article>  articles = new ArrayList<>();
		List<String> tags = new ArrayList<>();
		tags.add("t");
		tags.add("t2");
		tags.add("t3");
		tags.add("t");
		Article a1 = new Article("a","a",tags); 
		Article b1 = new Article("b","b",tags);
		Article c1 = new Article("a","c",tags);
		articles.add(a1);
		articles.add(b1);
		articles.add(c1);
		return articles;
	}

}


class Article {
	 
    private final String title;
    private final String author;
    private final List<String> tags;

    public Article(String title, String author, List<String> tags) {
        this.title = title;
        this.author = author;
        this.tags = tags;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public List<String> getTags() {
        return tags;
    }
}

class Department{
	String id;
	String name;
	
	public Department(String id,String name){
		this.id=id;
		this.name=name;
	}
}
class Employee{
	Department department;
	String id;
	String name;
	public Employee(Department department,String id,String name){
		this.department = department;
		this.id=id;
		this.name =name;
	}
	
}
